﻿using System.Collections.Generic;
using Engine;
using Engine.Graphics;

namespace Game
{
    public class WaterBucketsBlock : Block
    {
        public const int Index = 316;

        public Dictionary<Texture2D, BlockMesh> m_meshes = new Dictionary<Texture2D, BlockMesh>();

        public Dictionary<Texture2D, BlockMesh> m_meshes2 = new Dictionary<Texture2D, BlockMesh>();

        private BlockMesh m_standaloneBlockMesh = new BlockMesh();

        private BlockMesh m_drawBlockMesh = new BlockMesh();

        public Texture2D texture = ContentManager.Get<Texture2D>("Textures/items/st");

        public override void Initialize()
        {
            Model model = ContentManager.Get<Model>("Models/mt");
            Matrix boneAbsoluteTransform = BlockMesh.GetBoneAbsoluteTransform(model.FindMesh("mt").ParentBone);
            m_standaloneBlockMesh.AppendModelMeshPart(model.FindMesh("mt").MeshParts[0], boneAbsoluteTransform * Matrix.CreateTranslation(0.5f, 0f, 0.5f), makeEmissive: false, flipWindingOrder: false, doubleSided: false, flipNormals: false, Color.White);
            m_drawBlockMesh.AppendModelMeshPart(model.FindMesh("mt").MeshParts[0], boneAbsoluteTransform * Matrix.CreateTranslation(0f, 0f, 0f), makeEmissive: false, flipWindingOrder: false, doubleSided: false, flipNormals: false, Color.White);
            m_meshes.Add(texture, m_standaloneBlockMesh);
            m_meshes2.Add(texture, m_drawBlockMesh);
            base.Initialize();
        }

        public override void GenerateTerrainVertices(BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z)
        {
            foreach (KeyValuePair<Texture2D, BlockMesh> mesh in m_meshes)
            {
                generator.GenerateShadedMeshVertices(this, x, y, z, mesh.Value, Color.White, null, null, geometry.GetGeometry(mesh.Key).SubsetAlphaTest);
            }
        }

        public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
        {
            foreach (KeyValuePair<Texture2D, BlockMesh> item in m_meshes2)
            {
                BlocksManager.DrawMeshBlock(primitivesRenderer, item.Value, item.Key, Color.White, 1f * size, ref matrix, environmentData);
            }
        }
    }
}